<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  iphone
 */

class m_iphone_actions_IphoneMoveFolderContentAction extends f_core_Action
{
	public function execute()
	{
        $destFolderId = $this->getRequestParameter("folderId");
        $docId = $this->getRequestParameter("docId");
        $docLang = $this->getRequestParameter("docLang", $this->getContext()->getDefaultLang());
        $error = false;
        $msg = false;

		if(!is_null($destFolderId) && $this->getDocumentProvider()->checkDocument($destFolderId, $docLang))
		{
			$destFolder = $this->getDocumentProvider()
                               ->getByModelAndUniqueId("modules_backoffice/folder", $destFolderId, $docLang);

            if(!is_null($destFolder))
            {
                $relationManager = f_relation_Manager::getInstance();
                $dbProvider = f_database_Provider::getInstance();

                try
                {
                    $dbProvider->beginTransaction();

                    if($this->getDocumentProvider()->checkDocument($docId, $docLang))
                    {
                        $document = $this->getDocumentProvider()->getByUniqueId($docId, $docLang);
                        $sourceFolder = $relationManager->getParent($docId, $document->getDocumentTypeName());

                        $relationType = $document->getDocumentTypeName();
                        if($relationType == "media")
                        {
                            $moveError = $document->moveMedia($destFolder);
                        }
                        else
                        {
                            $relationManager->clearRelation($sourceFolder, $document, $relationType, $docLang);
                            $relationManager->setRelation($destFolder, $document, $relationType, null, $docLang);
                            $dbProvider->commit();
                        }
                    }
                    else
                    {
                        $error = true;
                    }
                }
                catch(BaseException $e)
                {
                    $dbProvider->rollBack($e);
                }
            }
            else
            {
                $error = true;
            }
		}
        else
        {
            $error = true;
            $msg =  $this->getLocale('backoffice.errors.required');
        }

        $xml = m_iphone_services_IphoneService::getInstance()->genericConfirm($error, $msg);
        return $xml;
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight("backoffice_accessBo");
	}
	
	public function errorHandler()
	{
		$this->setView('error');
	}
}